<template>
  <el-dialog
    v-model="visible"
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      :rules="dataRule"
      label-width="120px"
      @keyup.enter="onSubmit()"
    >
      <el-form-item
        label="1个月价格"
        prop="priceMonth1"
      >
        <el-input v-model="dataForm.priceMonth1" />
      </el-form-item>
      <el-form-item
        label="3个月价格"
        prop="priceMonth3"
      >
        <el-input v-model="dataForm.priceMonth3" />
      </el-form-item>
      <el-form-item
        label="12个月价格"
        prop="priceMonth12"
      >
        <el-input v-model="dataForm.priceMonth12" />
      </el-form-item>
      <el-form-item
        label="折扣"
        prop="discount"
      >
        <el-input v-model="dataForm.discount" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button
          type="primary"
          @click="onSubmit()"
        >确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { Debounce } from '@/utils/debounce'
const emit = defineEmits(['refreshDataList'])
const visible = ref(false)
const contentEnRef = ref(null)
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})

const options = ref([])
// eslint-disable-next-line no-unused-vars
const validateTitle = (rule, value, callback) => {
  console.log(value, 'value')
  if (!value.toString().trim()) {
    dataForm.value.priceMonth1 = ''
    callback(new Error('价格不能为空'))
  } else {
    callback()
  }
}
const dataRule = {
  priceMonth1: [
    { required: true, message: '价格不能为空', trigger: 'blur' },
    { validator: validateTitle, trigger: 'blur' }
  ],
  priceMonth3: [
    { required: true, message: '价格不能为空', trigger: 'blur' },
    { validator: validateTitle, trigger: 'blur' }
  ],
  priceMonth12: [
    { required: true, message: '价格不能为空', trigger: 'blur' },
    { validator: validateTitle, trigger: 'blur' }
  ]
}

const dataForm = ref({
  cardamom: null
})
const dataFormRef = ref(null)
const init = (id) => {
  dataForm.value.id = id || 0
  visible.value = true
  nextTick(() => {
    dataFormRef.value?.resetFields()
    if (dataForm.value.id) {
      http({
        url: http.adornUrl(
          '/admin/admin/vip/info/' + dataForm.value.id
        ),
        method: 'get',
        params: http.adornParams()
      }).then(({ data }) => {
        dataForm.value = data
      })
    }
  })
}
defineExpose({ init })

/**
 * 表单提交
 */
const onSubmit = Debounce(() => {
  console.log(dataForm.value, 'dataForm.value')
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      http({
        url: http.adornUrl('/admin/admin/vip'),
        method: dataForm.value.id ? 'put' : 'post',
        data: http.adornData(dataForm.value)
      }).then(() => {
        ElMessage({
          message: '操作成功',
          type: 'success',
          duration: 1500,
          onClose: () => {
            visible.value = false
            emit('refreshDataList')
            dataForm.value.content = ''
          }
        })
      })
    }
  })
})
</script>
